package com.rzico.report.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.report.entity.VirtualPledgeCountVo;
import com.rzico.report.entity.WaterTicketCountVo;
import com.rzico.report.service.VirtualPledgeCountService;
import com.rzico.report.service.WaterTicketCountService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 押金统计表
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-15
 */
@Api(description = "押金统计表")
@RestController
@RequestMapping("/report/VirtualPledgeReport")
public class VirtualPledgeReportController extends BaseController{

    @Autowired
    private VirtualPledgeCountService virtualPledgeCountService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 按客户分组统计
     *
     * @return
     */
    @ApiOperation("按客户分组统计")
    @GetMapping("/memberList")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户ID", dataType = "Long", paramType = "query")
    })
    public CommResult<VirtualPledgeCountVo> memberList(String employeeId, Long shopId, Long memberId,
                                                       Date startDate, Date endDate, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        Map<String, Object> params = new HashMap<String, Object>();
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (null != memberId){
            params.put("memberId", memberId);
        }

        params.put("enterpriseId", enterprise.getId());
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<VirtualPledgeCountVo> list = virtualPledgeCountService.selectMemberList(params);
        PageResult<VirtualPledgeCountVo> pageResult = new PageResult<VirtualPledgeCountVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 底部汇总合计
     *
     * @return
     */
    @ApiOperation("底部汇总合计")
    @GetMapping("/summary")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户ID", dataType = "Long", paramType = "query")
    })
    public CommResult<WaterTicketCountVo> summary(String employeeId, Long shopId, Long memberId,
                                                  Date startDate, Date endDate, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        Map<String, Object> params = new HashMap<String, Object>();
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (null != memberId){
            params.put("memberId", memberId);
        }

        params.put("enterpriseId", enterprise.getId());
         List<VirtualPledgeCountVo> list = virtualPledgeCountService.selectSummary(params);
        return CommResult.success(list);

    }

}
